package com.yulan.bankback.log;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
@Slf4j
public class Logger implements InitializingBean {

    @Value("${log.attack}")
    private String attackLogPath;

    @Value("${log.remote-login}")
    private String remoteLoginLogPath;

    @Value("${log.money-flow}")
    private String moneyFLowLogPath;

    @Value("${log.recharge-log}")
    private String rechargeLogPath;

    @Value("${log.abnormal-log}")
    private String abnormalLogPath;

    FileWriter attackFileWriter;

    FileWriter  remoteLoginFileWriter;

    FileWriter moneyFLowFileWriter;

    FileWriter rechargeFileWriter;

    FileWriter abnormalFileWriter;

    private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private static final SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");

    private static final SimpleDateFormat formatter3 = new SimpleDateFormat("MM-dd");


    @Override
    public void afterPropertiesSet() throws Exception {
        attackFileWriter = new FileWriter(this.attackLogPath, true);
        remoteLoginFileWriter = new FileWriter(this.remoteLoginLogPath, true);
        moneyFLowFileWriter = new FileWriter(this.moneyFLowLogPath, true);
        rechargeFileWriter = new FileWriter(this.rechargeLogPath, true);
        abnormalFileWriter = new FileWriter(this.abnormalLogPath, true);
        log.info(">>>>>>>>>>>>>>> 已完成日志组件初始化 >>>>>>>>>>>>>>>");
    }


    public void logAttack(String attackIp, String uid, String attackType, String attackLevel){
        String logContent = formatter.format(new Date()) + "|" + attackIp + "|"
                + uid + "|" + attackType + "|" + attackLevel;
        try {
            attackFileWriter.write(logContent + "\n");
            attackFileWriter.flush();
            log.info("检测到网络攻击事件, IP: {}, uid: {}, 类型: {}",attackIp, uid, attackType);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void logRemoteLogin(String uid, String ip, String city){
        String logContent = formatter.format(new Date()) + "|" + uid + "|" + ip + "|" + city;
        try {
            remoteLoginFileWriter.write(logContent + "\n");
            remoteLoginFileWriter.flush();
            log.info("检测到异地登陆事件, uid: {}, ip: {}, city: {}",uid, ip, city);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void logMoneyFlow(String srcUid, String destUid, String money, String type){
        String logContent = formatter.format(new Date()) + "|" + srcUid
                + "|" + destUid + "|" + money + "|" + type;
        try {
            moneyFLowFileWriter.write(logContent + "\n");
            moneyFLowFileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void logRecharge(String uid,  String money){
        String logContent = formatter.format(new Date()) + "|" + uid
                + "|" + money + "|recharge";
        try {
            rechargeFileWriter.write(logContent + "\n");
            rechargeFileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void logAbnormal(String uid, String type){
        String logContent = formatter.format(new Date()) + "|" + uid + "|" + type ;
        try {
            abnormalFileWriter.write(logContent + "\n");
            abnormalFileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}












